<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增财务收支记录')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-financial-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">财务记录类型：</label>
                    <div class="col-sm-8">
                        <select name="dictCode" id="financialRecordType" class="form-control m-b" th:with="type=${@dict.getType('sys_financial_record')}" required>
                            <option value="">-- 请选择 --</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12" id="employeeSalaryFields" style="display: none;">
                <div class="form-group">
                    <label class="col-sm-3 control-label">员工姓名：</label>
                    <div class="col-sm-8">
                        <select name="userName" id="employeeNameSelect" class="form-control m-b">
                            <option value="">-- 请选择员工 --</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">岗位：</label>
                    <div class="col-sm-8">
                        <input name="employeePosition" id="employeePositionInput" class="form-control" type="text" readonly>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">计算结果：</label>
                    <div class="col-sm-8">
                        <input name="employeeSalaryResult" id="employeeSalaryResultInput" class="form-control" type="text" readonly>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">记录日期：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="recordDate" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
<!--            <div class="col-xs-12">-->
<!--                <div class="form-group">-->
<!--                    <label class="col-sm-3 control-label">关联ID：</label>-->
<!--                    <div class="col-sm-8">-->
<!--                        <input name="relatedId" class="form-control" type="text">-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
<!--            <div class="col-xs-12">-->
<!--                <div class="form-group">-->
<!--                    <label class="col-sm-3 control-label is-required">金额：</label>-->
<!--                    <div class="col-sm-8">-->
<!--                        <input name="amount" class="form-control" type="text" required>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <input name="remark" class="form-control" type="text">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/financial"
        $("#form-financial-add").validate({
            focusCleanup: true
        });
        $(document).ready(function() {
            $('#financialRecordType').change(function() {
                var selectedType = $(this).val();
                // 隐藏员工工资相关字段
                $('#employeeSalaryFields').hide();
                if (selectedType === '1') {
                    $('#employeeSalaryFields').show();
                    // 加载员工列表
                    loadEmployeeList();
                }
            });

            // 员工工资计算示例
            $('input[name="relatedId"]').on('input', function() {
                var employeeId = $(this).val();
                if (employeeId) {
                    // 调用接口获取基本工资和奖金参数
                    $.ajax({
                        url: 'getEmployeeSalaryInfo/' + employeeId,
                        type: 'GET',
                        dataType: 'json',
                        success: function(data) {
                            var basicSalary = data.basicSalary;
                            var bonusRate = data.bonusRate;
                            var salary = basicSalary + basicSalary * bonusRate;
                            $('input[name="employeeSalaryResult"]').val(salary);
                            // 将计算结果赋值给金额输入框
                            $('input[name="amount"]').val(salary);
                        },
                        error: function() {
                            console.error('获取员工工资信息失败');
                        }
                    });
                }
            });

            function loadEmployeeList() {
                $.ajax({
                    url: '/system/user/list',
                    type: 'POST',
                    dataType: 'json',
                    cache: false,
                    success: function(data) {
                        console.log(data)
                        const rows = data.rows;
                        var employeeSelect = $('#employeeNameSelect');
                        employeeSelect.empty();
                        employeeSelect.append('<option value="">-- 请选择员工 --</option>');
                        $.each(rows, function(index, employee) {
                            employeeSelect.append('<option value="' + employee.userId + '">' + employee.userName + '</option>');
                        });
                    },
                    error: function() {
                        console.error('获取员工列表失败');
                    }
                });
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-financial-add').serialize());
            }
        }

        $("input[name='recordDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
</html>